package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class AdvimgController {
    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private AdvposService advposService;

    /**
     * 分页展示
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String advimgdesc,String advimgpos,Integer page,Integer limit){
        //构造分页构造器
        Page<Advimg> pageInfo=new Page<>(page,limit);
        //条件构造器
        LambdaQueryWrapper<Advimg> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(advimgdesc!=null,Advimg::getAdvimgdesc,advimgdesc)
                .like(advimgpos!=null,Advimg::getAdvimgpos,advimgpos);
        //添加排序条件
        queryWrapper.orderByDesc(Advimg::getId);
        //执行分页查询
         advimgService.page(pageInfo, queryWrapper);

         //Dto
        Page<AdvimgDto>advimgDtoPage=new Page<>();

        BeanUtils.copyProperties(pageInfo,advimgDtoPage,"records");

        List<Advimg> imgList=pageInfo.getRecords();

        List<AdvimgDto>advimgDtoList=imgList.stream().map(advimg->{
            AdvimgDto advimgDto = new AdvimgDto();
            BeanUtils.copyProperties(advimg,advimgDto);

            Advpos advpos = advposService.getById(advimg.getAdvimgpos());
            String advposname = advpos.getAdvposname();
            advimgDto.setAdvposname(advposname);
            return advimgDto;
        }).collect(Collectors.toList());

        long total = pageInfo.getTotal();
        long size = pageInfo.getSize();
        long current = pageInfo.getCurrent();

        return ResultData.ok("", PageResult.init(total,size,current, advimgDtoList));
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData findById(Long id){
        Advimg advimg = advimgService.getById(id);
        return ResultData.ok("",advimg);
    }

    /**
     * 修改数据
     * @param advimg
     * @return
     */
    @PostMapping("/update")
    public ResultData Update(@RequestBody Advimg advimg){
        String id = advimg.getId();
        advimgService.removeById(id);
        advimgService.save(advimg);
        return ResultData.ok("修改成功",null);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData Detel(Long id){
        advimgService.removeById(id);
        
        return ResultData.ok("删除成功",null);
    }

    /**
     * 添加数据
     * @param advimg
     * @return
     */
    @PostMapping("/add")
    public ResultData Add(@RequestBody Advimg advimg){
        advimgService.save(advimg);
        return ResultData.ok("添加成功",null);
    }
}
